package woaini.fenger.core.bot.base.impl;

import cn.hutool.core.map.MapBuilder;
import com.alibaba.fastjson2.*;
import lombok.extern.slf4j.Slf4j;
import woaini.fenger.core.bot.base.Bot;
import woaini.fenger.core.bot.communication.base.BaseBotRequest;
import woaini.fenger.core.bot.enums.BotStatusEnum;
import woaini.fenger.core.bot.enums.ConnectType;
import woaini.fenger.core.bot.enums.PostTypeEnums;
import woaini.fenger.core.bot.event.Messages;
import woaini.fenger.core.bot.event.message.GroupMessageEvent;
import woaini.fenger.core.bot.event.message.PrivateMessageEvent;

import woaini.fenger.core.bot.base.IBotAdapter;
import woaini.fenger.core.bot.event.BaseEvent;
import woaini.fenger.core.bot.id.ID;

import java.util.HashMap;
import java.util.Map;

/**
 * 基本机器人解码器 默认为OneBot实现
 *
 * @see OneBotAdapterImpl
 * @author yefeng {@code @Date} 2023-05-16 16:50:39
 */
@Slf4j
public class OneBotAdapterImpl extends IBotAdapter {

  public OneBotAdapterImpl(Bot bot) {
    super(bot);
  }

  @Override
  public BaseEvent decode(ConnectType connectType,String rawString) {
    // 先转换为基础消息 获取消息类型
    BaseEvent baseEvent = JSONObject.parseObject(rawString, BaseEvent.class);
    // 判断上报类型
    PostTypeEnums postType = baseEvent.getPostType();
    if (postType == null) {
      return null;
    }

    switch (postType) {
      case message:
        {
          // 获取消息类型 子类型
          String subType = (String) JSONPath.eval(rawString, "$.message_type");
          switch (subType) {
              // 私聊消息
            case "private":
              {
                return JSONObject.parseObject(rawString, PrivateMessageEvent.class);
              }
              // 群里消息
            case "group":
              {
                return JSONObject.parseObject(rawString, GroupMessageEvent.class);
              }
            default:
              {
                log.warn("消息类型不支持:{}", subType);
                return null;
              }
          }
        }
      case notice:
        {
          break;
        }
      case request:
        {
          break;
        }
      case meta_event:
        {
          break;
        }
      default:
        {
          break;
        }
    }
    return null;
  }

  @Override
  public void sendPrivateMessage(ID userId, Messages message, boolean autoEscape) {
    if (!checkBot()) {
      return;
    }
    Map<String, Object> params =
        MapBuilder.create(new HashMap<String, Object>())
            .put("user_id", userId.getLongValue())
            .put("message", message)
            .build();
    this.bot.send(new BaseBotRequest("send_private_msg", params));
  }

  @Override
  public void sendGroupMsg(ID groupId, Messages message, boolean autoEscape) {
    if (!checkBot()) {
      return;
    }
    Map<String, Object> params =
        MapBuilder.create(new HashMap<String, Object>())
            .put("group_id", groupId.getLongValue())
            .put("message", message)
            .build();
    this.bot.send(new BaseBotRequest("send_group_msg", params));
  }

  public void sendMessage(
      String messageType, ID userId, ID groupId, Messages messages, boolean autoEscape) {
    switch (messageType) {
      case "private":
        {
          sendPrivateMessage(userId, messages, autoEscape);
          break;
        }
      case "group":
        {
          sendGroupMsg(groupId, messages, autoEscape);
          break;
        }
      default:
        {
          log.warn("未知的消息类型:{}", messageType);
          break;
        }
    }
  }
}
